package com.atguigu.educms.controller.web;


import com.atguigu.common.R;
import com.atguigu.common.cron.bean.FrontPageBean;
import com.atguigu.common.jwt.JwtUtils;
import com.atguigu.educms.entity.CmsComment;
import com.atguigu.educms.entity.CmsContent;
import com.atguigu.educms.service.CmsCommentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 文章评论(前台) 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/educms/cms-comment-front")
public class CmsCommentFrontController {

    @Autowired
    private CmsCommentService commentService;

    @ApiOperation(value = "前台分页获取评论")
    @GetMapping("/getFrontComment/{pageNo}/{pageSize}")
    public R getFrontComment(@PathVariable("pageNo") Integer pageNo,
                             @PathVariable("pageSize") Integer pageSize,
                             @RequestParam("contentId") String contentId) {
        FrontPageBean frontPageBean = commentService.getFrontComment(pageNo, pageSize, contentId);
        return R.ok().data(frontPageBean);
    }

    @ApiOperation(value = "回复评论")
    @PostMapping("/commitComment")
    public R commitComment(@RequestBody CmsComment cmsComment, @ApiIgnore HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)){
            return R.error().code(28004).message("请登录");
        }
        commentService.commitComment(cmsComment);
        return R.ok();
    }

}
